1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.AbstractCollectionTester;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.WrongType;
26  import com.google.common.collect.testing.features.CollectionFeature;
27  import com.google.common.collect.testing.features.CollectionSize;
28  
29  import java.util.Arrays;
30  import java.util.List;
31  
32  /**
33   * A generic JUnit test which tests {@code toArray()} operations on a
34   * collection. Can't be invoked directly; please see
35   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
36   *
37   * @author Kevin Bourrillion
38   * @author Chris Povirk
39   */
40  @GwtCompatible(emulated = true)
41  public class CollectionToArrayTester<E> extends AbstractCollectionTester<E> {
42    public void testToArray_noArgs() {
43      Object[] array = collection.toArray();
44      expectArrayContentsAnyOrder(createSamplesArray(), array);
45    }
46  
47    /**
48     * {@link Collection#toArray(Object[])} says: "Note that
49     * <tt>toArray(new Object[0])</tt> is identical in function to
50     * <tt>toArray()</tt>."
51     *
52     * <p>For maximum effect, the collection under test should be created from an
53     * element array of a type other than {@code Object[]}.
54     */
55    public void testToArray_isPlainObjectArray() {
56      Object[] array = collection.toArray();
57      assertEquals(Object[].class, array.getClass());
58    }
59  
60    public void testToArray_emptyArray() {
61      E[] empty = getSubjectGenerator().createArray(0);
62      E[] array = collection.toArray(empty);
63      assertEquals("toArray(emptyT[]) should return an array of type T",
64          empty.getClass(), array.getClass());
65      assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
66      expectArrayContentsAnyOrder(createSamplesArray(), array);
67    }
68  
69    @CollectionFeature.Require(KNOWN_ORDER)
70    public void testToArray_emptyArray_ordered() {
71      E[] empty = getSubjectGenerator().createArray(0);
72      E[] array = collection.toArray(empty);
73      assertEquals("toArray(emptyT[]) should return an array of type T",
74          empty.getClass(), array.getClass());
75      assertEquals("toArray(emptyT[]).length:", getNumElements(), array.length);
76      expectArrayContentsInOrder(getOrderedElements(), array);
77    }
78  
79    public void testToArray_emptyArrayOfObject() {
80      Object[] in = new Object[0];
81      Object[] array = collection.toArray(in);
82      assertEquals("toArray(emptyObject[]) should return an array of type Object",
83          Object[].class, array.getClass());
84      assertEquals("toArray(emptyObject[]).length",
85          getNumElements(), array.length);
86      expectArrayContentsAnyOrder(createSamplesArray(), array);
87    }
88  
89    public void testToArray_rightSizedArray() {
90      E[] array = getSubjectGenerator().createArray(getNumElements());
91      assertSame("toArray(sameSizeE[]) should return the given array",
92          array, collection.toArray(array));
93      expectArrayContentsAnyOrder(createSamplesArray(), array);
94    }
95  
96    @CollectionFeature.Require(KNOWN_ORDER)
97    public void testToArray_rightSizedArray_ordered() {
98      E[] array = getSubjectGenerator().createArray(getNumElements());
99      assertSame("toArray(sameSizeE[]) should return the given array",
100         array, collection.toArray(array));
101     expectArrayContentsInOrder(getOrderedElements(), array);
102   }
103 
104   public void testToArray_rightSizedArrayOfObject() {
105     Object[] array = new Object[getNumElements()];
106     assertSame("toArray(sameSizeObject[]) should return the given array",
107         array, collection.toArray(array));
108     expectArrayContentsAnyOrder(createSamplesArray(), array);
109   }
110 
111   @CollectionFeature.Require(KNOWN_ORDER)
112   public void testToArray_rightSizedArrayOfObject_ordered() {
113     Object[] array = new Object[getNumElements()];
114     assertSame("toArray(sameSizeObject[]) should return the given array",
115         array, collection.toArray(array));
116     expectArrayContentsInOrder(getOrderedElements(), array);
117   }
118 
119   public void testToArray_oversizedArray() {
120     E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
121     array[getNumElements()] = samples.e3;
122     array[getNumElements() + 1] = samples.e3;
123     assertSame("toArray(overSizedE[]) should return the given array",
124         array, collection.toArray(array));
125 
126     List<E> subArray = Arrays.asList(array).subList(0, getNumElements());
127     E[] expectedSubArray = createSamplesArray();
128     for (int i = 0; i < getNumElements(); i++) {
129       assertTrue(
130           "toArray(overSizedE[]) should contain element " + expectedSubArray[i],
131           subArray.contains(expectedSubArray[i]));
132     }
133     assertNull("The array element "
134         + "immediately following the end of the collection should be nulled",
135         array[getNumElements()]);
136     // array[getNumElements() + 1] might or might not have been nulled
137   }
138 
139   @CollectionFeature.Require(KNOWN_ORDER)
140   public void testToArray_oversizedArray_ordered() {
141     E[] array = getSubjectGenerator().createArray(getNumElements() + 2);
142     array[getNumElements()] = samples.e3;
143     array[getNumElements() + 1] = samples.e3;
144     assertSame("toArray(overSizedE[]) should return the given array",
145         array, collection.toArray(array));
146 
147     List<E> expected = getOrderedElements();
148     for (int i = 0; i < getNumElements(); i++) {
149       assertEquals(expected.get(i), array[i]);
150     }
151     assertNull("The array element "
152         + "immediately following the end of the collection should be nulled",
153         array[getNumElements()]);
154     // array[getNumElements() + 1] might or might not have been nulled
155   }
156 
157   @CollectionSize.Require(absent = ZERO)
158   public void testToArray_emptyArrayOfWrongTypeForNonEmptyCollection() {
159     try {
160       WrongType[] array = new WrongType[0];
161       collection.toArray(array);
162       fail("toArray(notAssignableTo[]) should throw");
163     } catch (ArrayStoreException expected) {
164     }
165   }
166 
167   @CollectionSize.Require(ZERO)
168   public void testToArray_emptyArrayOfWrongTypeForEmptyCollection() {
169     WrongType[] array = new WrongType[0];
170     assertSame(
171         "toArray(sameSizeNotAssignableTo[]) should return the given array",
172         array, collection.toArray(array));
173   }
174 
175   private void expectArrayContentsAnyOrder(Object[] expected, Object[] actual) {
176     Helpers.assertEqualIgnoringOrder(
177         Arrays.asList(expected), Arrays.asList(actual));
178   }
179 
180   private void expectArrayContentsInOrder(List<E> expected, Object[] actual) {
181     assertEquals("toArray() ordered contents: ",
182         expected, Arrays.asList(actual));
183   }
184 }
185